import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/home',
    name:'resourcehomehome',
    meta: { auth:true },
    children: [{
      path: 'home',
      name: 'resourcehomehome',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home',affix:true}
    }]
  },
  // {
  //   path: '/datastatis',
  //   component: Layout,
  //   redirect: '/datastatis/statispage',
  //   name:'resourcehomehome',
  //   meta: { auth:true },
  //   children: [{
  //     path: 'statispage',
  //     name: 'statispagepage',
  //     component: () => import('@/views/home/index'),
  //     meta: { title: '数据统计', icon: 'home',affix:true}
  //   },{
  //     path: 'homedetail',
  //     name: 'resourcehomehomedetail',
  //     component: () => import('@/views/home/detail'),
  //     meta: { title: '统计详情',activeMenu:'/datastatis'},
  //     hidden:true,
  //   }]
  // },
  
  {
    path: '/usermgt',
    component: Layout,
    redirect: 'userindex',
    name:'accoutuserindex',
    meta: { title: '用户管理', icon: 'usermgt' },
    children: [
      {
        path: 'userindex',
        component: () => import('@/views/user/list'),
        name: 'accoutuserlist',
        meta: { title: '用户管理' }
      },
      // {
      //   path: 'userrole',
      //   component: () => import('@/views/user/role'),
      //   name: 'accoutuserrole',
      //   meta: { title: '角色管理' }
      // },
      // {
      //   path: 'userrules',
      //   component: () => import('@/views/user/rules'),
      //   name: 'accoutuserrules',
      //   meta: { title: '拉取规则' }
      // }
    ]
  },
 
  {
    path: '/approvalmgt',
    component: Layout,
    redirect: 'waitapproval',
    name:'approvalmgtindex',
    meta: { title: '审批管理', icon: 'approve' },
    children: [
      {
        path: 'waitapproval',
        component: () => import('@/views/resource/user'),
        name: 'approvalmgtwait',
        meta: { title: '资源待审核' }
      },
      {
        path: 'waitapproval/check',
        name: 'approvalmgtcheck',
        component: () => import('@/views/commdetail/index'),
        meta: { title: '资源审核',activeMenu:'/approvalmgt/waitapproval' },
        hidden:true,
      },
      {
        path: 'unpassapproval',
        component: () => import('@/views/approval/unpass'),
        name: 'approvalmgtunpass',
        meta: { title: '资源审核未通过' }
      },
      {
        path: 'unpassapproval/detail',
        name: 'approvalmgtdetail',
        component: () => import('@/views/commdetail/index'),
        meta: { title: '查看详情',activeMenu:'/approvalmgt/unpassapproval' },
        hidden:true,
      },
      {
        path: 'passapproval',
        component: () => import('@/views/approval/pass'),
        name: 'approvalmgtpass',
        meta: { title: '资源审核通过' }
      },
      {
        path: 'passapproval/detail',
        name: 'approvalmgtdetailpass',
        component: () => import('@/views/commdetail/index'),
        meta: { title: '查看详情',activeMenu:'/approvalmgt/passapproval' },
        hidden:true,
      },
      {
        path: 'history',
        component: () => import('@/views/approval/historylist'),
        name: 'approvalmgthistory',
        meta: { title: '历史资源临时库' }
      },
      {
        path: 'history/detail',
        component: () => import('@/views/commdetail/index'),
        name: 'approvalmgtdetailhistory',
        meta: { title: '历史查看详情' ,activeMenu:'/approvalmgt/history'},
        hidden: true
      },
      // {
      //   path: 'recycle',
      //   component: () => import('@/views/resource/approval'),
      //   name: 'approvalmgtrecycle',
      //   meta: { title: '回收站' }
      // },
    ]
  },
  {
    path: '/rule',
    component: Layout,
    redirect: 'ruleindex',
    name:'rule',
    meta: { title: '活动规则', icon: 'dashboard' },
    children: [
      {
        path: 'ruleindex',
        component: () => import('@/views/rule/index'),
        name: 'ruleindex',
        meta: { title: '基本信息' }
      },
      {
        path: 'resourcelist',
        component: () => import('@/views/rule/list'),
        name: 'resourcemgtlist',
        meta: { title: '操作记录' }
      },
    ]
  },
  {
    path: '/wthdrawal',
    component: Layout,
    redirect: 'waitapproval',
    name:'wthdrawalmgtindex',
    meta: { title: '提现', icon: 'tixian' },
    children: [
      {
        path: 'wthdrawalindex',
        component: () => import('@/views/withdrawal/list'),
        name: 'wthdrawalmgttx',
        meta: { title: '提现' }
      },
    ]
  },

  // {
  //   path: 'external-link',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
  //       meta: { title: 'External Link', icon: 'link' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true, meta:{ auth:true } }
]


const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()



// 重写addRoutes
// const addRoutes = Router.prototype.addRoutes;
// Router.prototype.addRoutes = function (newRoutes) {
//   router.matcher = createRouter(constantRoutes).matcher; //通过重置matcher来重置router
//   addRoutes.call(this, newRoutes); //调用原有方法
// };


// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


// 防止连续点击多次路由报错
// let routerPush = Router.prototype.push;
// Router.prototype.push = function push(location) {
//   return routerPush.call(this, location).catch(err => err)
// }

export default router
